using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class HealthBar : BasePoolElement
{
    // Start is called before the first frame update
    public Image bg, fill;
    RectTransform rectTransform;
    Vector2 size;

    private void Awake()
    {
        if (rectTransform == null) rectTransform = GetComponent<RectTransform>();

        bg = transform.GetChild(0).GetComponent<Image>();
        fill = transform.GetChild(1).GetComponent<Image>();

        SetSize(new Vector2(300, 20));
    }

    public override void PoolInit()
    {
        base.PoolInit();
        transform.rotation = Camera.main.transform.rotation;
    }

    public void ShowHealth(float hp,float maxhp) {
        fill.rectTransform.sizeDelta = new Vector2( (float) hp / (float) maxhp * size.x , size.y);
    }

    public void SetParent(Transform parent) {
        transform.SetParent(UIManager.Instance.ThreeDCanvas.transform);
        transform.localScale = Vector3.one;
    }

    public void SetSize(Vector2 size) {
        this.size = size;
        rectTransform.sizeDelta = size;
        bg.rectTransform.sizeDelta = size + Vector2.one*2;
        fill.rectTransform.sizeDelta = size;
        bg.rectTransform.anchoredPosition = new Vector2(-1, 0);
        fill.rectTransform.anchoredPosition = new Vector2(0, 0);
    }

}
